home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / bind493a.zip / named / named-xfer.c < prev    next >
C/C++ Source or Header  |  1995-12-06  |  39KB  |  1,634 lines

  1. /*
  2.  * The original version of xfer by Kevin Dunlap.
  3.  * Completed and integrated with named by David Waitzman
  4.  *    (dwaitzman@bbn.com) 3/14/88.
  5.  * Modified by M. Karels and O. Kure 10-88.
  6.  * Modified extensively since then by just about everybody.
  7.  */
  8.  
  9. /*
  10.  * ++Copyright++ 1988, 1990
  11.  * -
  12.  * Copyright (c) 1988, 1990
  13.  *    The Regents of the University of California.  All rights reserved.
  14.  * 
  15.  * Redistribution and use in source and binary forms, with or without
  16.  * modification, are permitted provided that the following conditions
  17.  * are met:
  18.  * 1. Redistributions of source code must retain the above copyright
  19.  *    notice, this list of conditions and the following disclaimer.
  20.  * 2. Redistributions in binary form must reproduce the above copyright
  21.  *    notice, this list of conditions and the following disclaimer in the
  22.  *    documentation and/or other materials provided with the distribution.
  23.  * 3. All advertising materials mentioning features or use of this software
  24.  *    must display the following acknowledgement:
  25.  *     This product includes software developed by the University of
  26.  *     California, Berkeley and its contributors.
  27.  * 4. Neither the name of the University nor the names of its contributors
  28.  *    may be used to endorse or promote products derived from this software
  29.  *    without specific prior written permission.
  30.  * 
  31.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  32.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  35.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  41.  * SUCH DAMAGE.
  42.  * -
  43.  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  44.  * 
  45.  * Permission to use, copy, modify, and distribute this software for any
  46.  * purpose with or without fee is hereby granted, provided that the above
  47.  * copyright notice and this permission notice appear in all copies, and that
  48.  * the name of Digital Equipment Corporation not be used in advertising or
  49.  * publicity pertaining to distribution of the document or software without
  50.  * specific, written prior permission.
  51.  * 
  52.  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  53.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  54.  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  55.  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  56.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  57.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  58.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  59.  * SOFTWARE.
  60.  * -
  61.  * --Copyright--
  62.  */
  63.  
  64. #if !defined(lint) && !defined(SABER)
  65. char copyright[] =
  66. "@(#) Copyright (c) 1988, 1990 The Regents of the University of California.\n\
  67.  portions Copyright (c) 1993 Digital Equipment Corporation\n\
  68.  All rights reserved.\n";
  69. #endif /* not lint */
  70.  
  71. #if !defined(lint) && !defined(SABER)
  72. static char sccsid[] = "@(#)named-xfer.c    4.18 (Berkeley) 3/7/91";
  73. static char rcsid[] = "$Id: named-xfer.c,v 8.10 1995/12/06 20:34:38 vixie Exp $";
  74. #endif /* not lint */
  75.  
  76. #include <sys/param.h>
  77. #include <sys/file.h>
  78. #include <sys/stat.h>
  79. #include <sys/socket.h>
  80.  
  81. #include <netinet/in.h>
  82. #if defined(__osf__)
  83. # include <sys/mbuf.h>
  84. # include <net/route.h>
  85. #endif
  86. #if defined(_AIX)
  87. # include <sys/time.h>
  88. # define TIME_H_INCLUDED
  89. #endif
  90. #include <net/if.h>
  91. #include <netdb.h>
  92. #include <arpa/inet.h>
  93. #include <arpa/nameser.h>
  94.  
  95. #include <errno.h>
  96. #include <resolv.h>
  97. #include <stdio.h>
  98. #include <syslog.h>
  99. #if !defined(SVR4) || !defined(sun)
  100. # include <math.h>
  101. #endif
  102. #include <ctype.h>
  103. #include <signal.h>
  104.  
  105. #define MAIN_PROGRAM
  106. #include "named.h"
  107. #undef MAIN_PROGRAM
  108.  
  109. #ifndef LOG_PERROR
  110. # define LOG_PERROR 0
  111. #endif
  112.  
  113. static    struct zoneinfo    zone;        /* zone information */
  114.  
  115. static    char        ddtfilename[] = _PATH_TMPXFER,
  116.             *ddtfile = ddtfilename,
  117.             *tmpname,
  118.             *domain;        /* domain being xfered */
  119.  
  120. static    int        quiet = 0,
  121.             read_interrupted = 0,
  122.             curclass,
  123.             domain_len;        /* strlen(domain) */
  124.  
  125. static    FILE        *fp = NULL,
  126.             *dbfp = NULL;
  127.  
  128. static    char        *ProgName;
  129.  
  130. static    void        usage __P((const char *));
  131. static    int        getzone __P((struct zoneinfo *, u_int32_t, int)),
  132.             print_output __P((u_char *, int, u_char *)),
  133.             netread __P((int, char *, int, int));
  134. static    SIG_FN        read_alarm __P(());
  135. static    const char    *soa_zinfo __P((struct zoneinfo *, u_char *, u_char*));
  136.  
  137. extern char *optarg;
  138. extern int optind, getopt();
  139.  
  140. void
  141. main(argc, argv)
  142.     int argc;
  143.     char *argv[];
  144. {
  145.     register struct zoneinfo *zp;
  146.     register struct hostent *hp;
  147.      char *dbfile = NULL, *tracefile = NULL, *tm = NULL;
  148.     int dbfd, ddtd, result, c, fd, closed = 0;
  149.     u_int32_t serial_no = 0;
  150.     u_int16_t port = htons(NAMESERVER_PORT);
  151.     struct stat statbuf;
  152. #ifdef STUBS
  153.     int stub_only = 0;
  154. #endif
  155. #ifdef GEN_AXFR
  156.     int class = C_IN;
  157. #endif
  158.  
  159.     if (ProgName = strrchr(argv[0], '/'))
  160.         ProgName++;
  161.     else
  162.         ProgName = argv[0];
  163.  
  164.     (void) umask(022);
  165.  
  166.     /* this is a hack; closing everything in the parent is hard. */
  167.     for (fd = getdtablesize()-1;  fd > STDERR_FILENO;  fd--)
  168.         closed += (close(fd) == 0);
  169.  
  170. #ifdef RENICE
  171.     nice(-40);    /* this is the recommended procedure to        */
  172.     nice(20);    /*   reset the priority of the current process */
  173.     nice(0);    /*   to "normal" (== 0) - see nice(3)          */
  174. #endif
  175.  
  176. #ifdef LOG_DAEMON
  177.     openlog(ProgName, LOG_PID|LOG_CONS|LOG_PERROR, LOGFAC);
  178. #else
  179.     openlog(ProgName, LOG_PID);
  180. #endif
  181. #ifdef STUBS
  182.     while ((c = getopt(argc, argv, "C:d:l:s:t:z:f:p:P:qS")) != EOF)
  183. #else
  184.     while ((c = getopt(argc, argv, "C:d:l:s:t:z:f:p:P:q")) != EOF)
  185. #endif
  186.         switch (c) {
  187. #ifdef GEN_AXFR
  188.             case 'C':
  189.             class = get_class(optarg);
  190.             break;
  191. #endif
  192.         case 'd':
  193. #ifdef DEBUG
  194.             debug = atoi(optarg);
  195. #endif
  196.             break;
  197.         case 'l':
  198.             ddtfile = (char *)malloc(strlen(optarg) +
  199.                          sizeof(".XXXXXX") + 1);
  200.             if (!ddtfile)
  201.                 panic(errno, "malloc(ddtfile)");
  202. #ifdef SHORT_FNAMES
  203.             filenamecpy(ddtfile, optarg);
  204. #else
  205.             (void) strcpy(ddtfile, optarg);
  206. #endif /* SHORT_FNAMES */
  207.             (void) strcat(ddtfile, ".XXXXXX");
  208.             break;
  209.         case 's':
  210.             serial_no = strtoul(optarg, (char **)NULL, 10);
  211.             break;
  212.         case 't':
  213.             tracefile = optarg;
  214.             break;
  215.         case 'z':        /* zone == domain */
  216.             domain = optarg;
  217.             domain_len = strlen(domain);
  218.             while ((domain_len > 0) && 
  219.                     (domain[domain_len-1] == '.'))
  220.                 domain[--domain_len] = '\0';
  221.             break;
  222.         case 'f':
  223.             dbfile = optarg;
  224.             tmpname = (char *)malloc((unsigned)strlen(optarg) +
  225.                          sizeof(".XXXXXX") + 1);
  226.             if (!tmpname)
  227.                 panic(errno, "malloc(tmpname)");
  228. #ifdef SHORT_FNAMES
  229.             filenamecpy(tmpname, optarg);
  230. #else
  231.             (void) strcpy(tmpname, optarg);
  232. #endif /* SHORT_FNAMES */
  233.             break;
  234.         case 'p':
  235.             port = htons((u_int16_t)atoi(optarg));
  236.             break;
  237.         case 'P':
  238.             port = (u_int16_t)atoi(optarg);
  239.             break;
  240. #ifdef STUBS
  241.         case 'S':
  242.             stub_only = 1;
  243.             break;
  244. #endif
  245.         case 'q':
  246.             quiet++;
  247.             break;
  248.         case '?':
  249.         default:
  250.             usage("unrecognized argument");
  251.             /* NOTREACHED */
  252.         }
  253.  
  254.     if (!domain || !dbfile || optind >= argc) {
  255.         if (!domain)
  256.             usage("no domain");
  257.         if (!dbfile)
  258.             usage("no dbfile");
  259.         if (optind >= argc)
  260.             usage("not enough arguments");
  261.         /* NOTREACHED */
  262.     }
  263.     if (stat(dbfile, &statbuf) != -1 &&
  264.         !S_ISREG(statbuf.st_mode) &&
  265.         !S_ISFIFO(statbuf.st_mode))
  266.         usage("dbfile must be a regular file or FIFO");
  267.     if (tracefile && (fp = fopen(tracefile, "w")) == NULL)
  268.         perror(tracefile);
  269.     (void) strcat(tmpname, ".XXXXXX");
  270.     /* tmpname is now something like "/etc/named/named.bu.db.XXXXXX" */
  271.     if ((dbfd = mkstemp(tmpname)) == -1) {
  272.         perror(tmpname);
  273.         if (!quiet)
  274.             syslog(LOG_ERR, "can't make tmpfile (%s): %m\n",
  275.                    tmpname);
  276.         exit(XFER_FAIL);
  277.     }
  278. #i